package com.mtw.bbs.bbsUser.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtw.bbs.bbsUser.pojo.entity.ResourceAPI;
import com.mtw.bbs.common.core.constant.ServiceConstant;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.userAdmin.pojo.vo.ResourceVo;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.mtw.bbs.bbsUser.service.ResourceAPIService;

import java.util.HashSet;
import java.util.List;

/**
 * 资源接口层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("admin/resource")
public class ResourceAPIController {


    private final ResourceAPIService resourceAPIService;



    @ApiOperation(value = "新增资源", notes = "新增一个资源")
    @PostMapping("/save")
    public Result add(@RequestBody ResourceAPI entity) {

        return Result.success(resourceAPIService.save(entity));
    }


    @ApiOperation(value = "删除资源", notes = "根据url的id来指定删除对象")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(resourceAPIService.removeById(id));
    }



    @ApiOperation(value = "删除资源", notes = "根据ids来指定删除对象")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestBody String[] ids) {
        return Result.success(resourceAPIService.delete(ids));
    }




    @ApiOperation(value = "修改资源", notes = "修改指定资源信息")
    @PutMapping(value = "/update")
    public Result update(@RequestBody ResourceAPI entity) {

        return Result.success(resourceAPIService.updateById(entity));
    }





    @ApiOperation(value = "获取资源", notes = "获取指定资源信息")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        return Result.success(resourceAPIService.getById(id));
    }







    @ApiOperation(value = "查询角色的资源", notes = "根据角色id查询角色所拥有的资源信息")
    @GetMapping(value = "/role")
    public Result queryResourceByRole(@RequestParam String roleId) {
        HashSet<String> set = new HashSet<>(1);
        set.add(roleId);
        return Result.success(resourceAPIService.queryByRoleId(set));
    }





    @ApiOperation(value = "查询所有启用的资源", notes = "查询所有启用的资源")
    @GetMapping(value = "/getAll")
    public Result queryAll() {

        LambdaQueryWrapper<ResourceAPI> lqw = new LambdaQueryWrapper<>();
        lqw.select(ResourceAPI::getId,ResourceAPI::getName);
        lqw.eq(ResourceAPI::getStatus, ServiceConstant.STATUS_ENABLE);
        List<ResourceAPI> list = resourceAPIService.list(lqw);
        return Result.success(list.stream().map(ResourceAPI::toVo).toList());
    }




    @ApiOperation(value = "分页查询资源", notes = "根据条件分页查询资源信息")
    @GetMapping(value = "/page")
    public Result query(@RequestParam(name = "page") int page,
                        @RequestParam(name = "size") int size,
                        @RequestParam(name = "name",required = false) String name,
                        @RequestParam(name = "code",required = false) String code,
                        @RequestParam(name = "url",required = false) String url
    ) {

        LambdaQueryWrapper<ResourceAPI> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), ResourceAPI::getName,name);
        lqw.like(StringUtils.isNotBlank(code), ResourceAPI::getCode,code);
        lqw.like(StringUtils.isNotBlank(url), ResourceAPI::getUrl,url);
        lqw.orderByDesc(ResourceAPI::getCreatedDate);
        Page<ResourceAPI> apiPage = resourceAPIService.page(new Page<>(page, size), lqw);
        Page<ResourceVo> voPage = new Page<>(apiPage.getCurrent(), apiPage.getSize(), apiPage.getTotal());
        voPage.setRecords(apiPage.getRecords().stream().map(ResourceAPI::toVo).toList());

        return Result.success(voPage);
    }


}
